//获取应用实例
let WxParse = require('../../../wxParse/wxParse.js');
let app = getApp();
let httptool=require("../../../utils/zry.httptool.js");
Page({
  data: {
    selectPerson:true,//隐藏区域
    selectArea:false,
    tmpImgswall:'',//照片墙
    customer: {},
    educationList:[],//学历列表
    sexopen:false, //男女下拉列表默认没有展开
    sexshow:false,
    eudopen:false,//学历下拉列表默认没展开
    eudshow:false,
    marropen:false,
    marrshow:false,
    addropen:false,
    areashow:true,
    condition: false,
    needuploadcount:0,
    uploadcount:0,
    tempFilePaths:'',//头像
    WXQRCode:'../../../images/moupload.png',//二维码
    msgprop:'',//消息提示 
    delArea:true,
 //   PwdTwo:'',/*确认密码*/
  //  Pwd:''
  },
  //头像上传
  uploadimg:function(){
    var that = this;  
     that.setData({  
         tempFilePaths:app.getStorage('headImg')
      });
    //上传头像
    wx.chooseImage({  
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {  
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
          that.setData({  
            tempFilePaths:res.tempFilePaths  
          });        
      }  
    });
//上传头像结束
  },
  uploadImgwall:function(){//上传照片墙
    var that = this;  
    wx.chooseImage({
      count: 9 - that.data.customer.ImgList.Length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res){
        for (let i= 0; i < res.tempFilePaths.length;i++)
        {
          that.data.customer.ImgList.push(res.tempFilePaths[i]); 
        }  
        that.setData({ customer: that.data.customer }) 
      }
    })
  },
  uploadWXcode:function(){//上传微信二维码
    var that = this;  
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res){
        that.data.customer.WXQRCode = res.tempFilePaths[0];
        that.setData({ WXQRCode:res.tempFilePaths[0]});
      }
    })
  },
  choosearea: function(e) {
     let that=this;
      that.setData({
      condition:!that.data.condition
      })
  },
  choosedClose:function(e){//城市确定选值
      let that=this;
      that.setData({
        condition:!that.data.condition,
        province:e.target.dataset.name
      })
  },
  //选项下拉列表展开事件
  showSex: function (e) {
    let sexopen = this.data.sexopen;
    this.hideSelect();
    if (sexopen) {
      this.setData({ sexshow: true, sexopen: false })
    } else {
      this.setData({ sexopen: true });
    }
  },
  showeducation: function (e) {
    let eudopen = this.data.eudopen;
    this.hideSelect();
    if (eudopen){
      this.setData({ eudshow: true, eudopen: false})
    } else {
      this.setData({ eudopen: true });
    }
  },
  showmarry:function(e){
    let marropen = this.data.marropen;
    this.hideSelect();
    if (marropen){
      this.setData({ marrshow: true, marropen: false});
    }else{
      this.setData({ marropen: true });
    }
  },
  hideSelect:function(){
    this.setData({
      sexopen: false, sexshow: false, eudopen: false, eudshow: false,
      pxopen: false, marropen: false, marrshow: false, addropen: false,
    });
  },
  mySelect:function(e){//子列表选项切换
    let that=this;
    let customer = this.data.customer;
    if (e.target.dataset.marry!=undefined)
      customer.Marriage = e.target.dataset.marry;
    if (e.target.dataset.sex != undefined)
      customer.Sex = e.target.dataset.sex == "true";
    if (e.target.dataset.name != undefined)
      customer.Education = e.target.dataset.name;
    that.setData({customer: this.data.customer});
    that.hideSelect();
  },
  showCity:function(e){
      let that=this;
      let provinceId=e.target.dataset.id;
      console.log(provinceId);
      httptool.ajaxPost("/api/City/GetCity",{ProvinceID:provinceId},function(res){
          if(res.suc){
             that.data.customer.ProvinceId = provinceId;
             that.setData({
                citys:res.data,
                province:e.target.dataset.name,
                
            });
          }
      });
  },
  ageChange:function(e){
    this.data.customer.Age = e.detail.value;
    this.setData({ customer: this.data.customer });
  },
  weightChange: function (e) {
    this.data.customer.Weight = e.detail.value;
    this.setData({ customer: this.data.customer });
  },
  changeDescription: function(e){
    this.data.customer.Description = e.detail.value;
    this.setData({ customer: this.data.customer });
  },
  delWXcode:function(e){//删除二维码
    var v = e.currentTarget.dataset.id;
    var customer = this.data.customer;
    customer.WXQRCode=null;
    this.setData({ WXQRCode:'../../../images/moupload.png',customer:customer});

  },
  delImgwall:function(e){
    var v = e.currentTarget.dataset.idx;
    var customer = this.data.customer;
    customer.ImgList.splice(v, 1);
    this.setData({ customer: customer });
  },
  heightChange:function(e){
    this.data.customer.Height=e.detail.value;
    this.setData({ customer: this.data.customer });
  },
  pwdChange:function(e){
    this.data.customer.Pwd=e.detail.value;
    this.setData({ customer: this.data.customer});
  },
  pwdTwoChange:function(e){
      this.data.customer.PwdTwo=e.detail.value;
      this.setData({customer: this.data.customer});
  },
  upload:function(i){
    let self=this;
    let filepath = i > -1 ? self.data.customer.ImgList[i] : self.data.customer.WXQRCode;
    console.log("图片路径为：" + filepath);
    let rootUrl = httptool.getRootUrl();
    wx.uploadFile({
      url: rootUrl + 'api/ImgUpload/ImgUploadAsync', //开发者服务器 url
      filePath: filepath,//要上传文件资源的路径
      name: 'file', //文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
      formData: { //HTTP 请求中其他额外的 form data
        savepath: "File\\PersonCenter\\",
        token: "62ccad6d-9611-41c5-96f6-08ec0ada0f68"
      },
      success: function (res) {
        var data = JSON.parse(res.data);
        if (data.suc) {
          if (i > -1) {
            self.data.customer.ImgList[i] = rootUrl + data.data;
            self.setData({ ImgList: self.data.ImgList });
            if (i == self.data.customer.ImgList.length - 1) {
              if (self.data.customer.WXQRCode == null ||
                self.data.customer.WXQRCode == undefined || self.data.customer.WXQRCode == "" ||
                self.data.customer.WXQRCode.indexOf(rootUrl) == 0 )   
              {
                self.updatecustomer(self);
              }else{
                self.upload(-1);
              }
            } else {
              i++;
              self.uploadphotowall(i);
            }
          } else {
            self.data.customer.WXQRCode = rootUrl + data.data;
            self.setData({ ImgList: self.data.ImgList });
            self.updatecustomer(self);
          }  
        }
      }
    })
  },
  uploadphotowall: function (i){
    let self = this;
    let rootUrl = httptool.getRootUrl();
    let ImgList = self.data.customer.ImgList;
    console.log("图片路径为：" + ImgList[i]);
    if (ImgList[i].indexOf("http://tmp") == 0 || ImgList[i].indexOf("wxfile://") == 0) {
      self.upload(i);
    } else if (ImgList.length - 1 > i) {
      i++;
      self.uploadphotowall(i);
    }else if (self.data.customer.WXQRCode == null ||
      self.data.customer.WXQRCode == undefined || self.data.customer.WXQRCode == ""||
      self.data.customer.WXQRCode.indexOf(rootUrl) == 0) {
      self.updatecustomer(self);
    } else {
      self.upload(-1);
    }  
  },
  saveEditinfo:function(e){
    let that=this;
    let imglist = that.data.customer.ImgList;
    let rootUrl = httptool.getRootUrl();
    if (imglist.length>0){
      that.uploadphotowall(0);
    }
    else if(that.data.customer.WXQRCode == null ||
      that.data.customer.WXQRCode == undefined || that.data.customer.WXQRCode == "" || 
      that.data.customer.WXQRCode.indexOf(rootUrl)==0) {
      self.updatecustomer(self);
    } else {
      self.upload(-1);
    }  
  },
  updatecustomer:function(that){
    httptool.ajaxPost("/api/Customer/EditCustomer", that.data.customer, function (res) {
      if (res.suc) {
        app.showNotify(that,"保存成功！",function(){
          wx.navigateBack(); 
        });        
      } else {
        app.showNotify(that, res.msg);
      }
    });
  },
  onLoad: function () {
    let that = this;
     this.setData({
      tempFilePaths:app.getStorage('headImg')
    });
    /*获取学历*/
     httptool.ajaxPost("/api/Common/GetEducations",'',function(res){
        if(res.suc){
            let data = app.notNull(res.data, []);
            that.setData({ educationList: data, pwdTwo: data.Pwd});
        }
    });
     /*获取个人信息*/
   httptool.ajaxPost("/api/Customer/GetMyPageInfo", { UserId: app.getStorage("customerId")}, 
     function (res) {
       if (res.suc) {
         let data = app.notNull(res.data, {});
         let wxQRCode = data.WXQRCode == "" || data.WXQRCode == null ?
          "../../../images/moupload.png" : data.WXQRCode;
         that.setData({ customer: data, WXQRCode: wxQRCode});
       }
     });    
  }
})